﻿
namespace HSS.Xaml.UI.Controls
{
	#region Using Directives
	using System.Collections.Generic;
	using HSS.ComponentModel;



	#endregion

	#region EnumValueEditor
	/// <summary>
	/// An editor for an <see cref="System.Enum"/>.
	/// </summary>
	/// <seealso cref="System.Enum"/>
	public class EnumValueEditor : ComboBoxEditorBase
	{
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="descriptor">The <see cref="HSSPropertyDescriptor"/> being edited.</param>
		public EnumValueEditor(HSSPropertyDescriptor descriptor)
			: base(descriptor)
		{
		}

		/// <summary>
		/// Gets the items for the ComboBox.
		/// </summary>
		/// <returns>The items for the ComboBox.</returns>
		protected override IEnumerable<object> GetListItems()
		{
			return EnumHelper.GetValues(Descriptor.PropertyType);
		}
		/// <summary>
		/// Excecutes the default action.
		/// </summary>
		public override void ExecuteDefaultAction()
		{
			if (this.Control.Items.Count > 0 && !this.Descriptor.IsReadOnly)
			{
				this.SyncValue(this.Object);
				var indexItem = 0;
				if (this.Control.SelectedIndex >= (this.Control.Items.Count - 1))
					indexItem = 0;
				else
					indexItem = this.Control.SelectedIndex + 1;

				var items = this.Control.ItemsSource as List<ComboBoxItemObject>;
				this.Object = items[indexItem].ObjectValue;
				this.EndEdit();
			}
		}

	}
	#endregion
}
